import user from '../../../utils/user.js'
import {
    createClientQrCode,
    
    // getInvitePeople
} from '../../../api/app.js'
import { saveQrcodeCache } from '../../api/insurance'
import {
    base64ToWxfile,
    // dateFormat 
} from '../../../utils/util.js'
import Canvas from '../../../utils/canvas.js'
const app = getApp()
Page({
    data: {
        postStatus: true,
        supplierName: '',
        supplierId: '',
        coord: '',
        count: 0,
        workers: [],
        showPosterStyle: false,
        record: true
    },
    async onReady() {
        user.waitLogin().then(async () => {
            this.setData({
                name: user.userName,
                postStatus: true
            })
        })

    },
    onLoad(option) {
        this.setData({
            supplierName: option.projectName.split('有限公司')[0],
            insuranceId: option.projectId,
            coord: option.coord
        })
        this.createInviteQrCode()
    },
    onUnload(){
        clearInterval(this.data.timer)
    },
    createInviteQrCode() {
        saveQrcodeCache({
            key: 'insurance',
            projectId: this.data.insuranceId,
            data: 'INID=' + this.data.insuranceId + '&UID=' + user.userId + '&coord=' + this.data.coord
        }).then(res=>{
            console.log(res,'到这一步啦吗')
            createClientQrCode({
                page: 'pages/join/join',
                scene: 'cacheId='+res,
                width: 280
            }).then(res => {
                var qrCode = res // 后台返回的base64图片流
                base64ToWxfile(qrCode, Math.ceil(Math.random() * 10000000000) + '.png').then(path => {
                    this.setData({
                        qrcode: path
                    })
                    this.saveQrCode()
                })
            })
        })

    },
    saveQrCode() {
        const ctx = wx.createCanvasContext('myCanvas')
        const canvas = new Canvas(ctx)
        ctx.setFillStyle('#fff')
        canvas.drawHeightCutImage(0, 0, 320, 500, 455, '/assets/img/poster/insurance.png')

        // 字体
        ctx.font =
		'normal normal 20px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
	
        // 邀请内容
        canvas.fillText(user.userName + ' 邀请您加入', 22, 90, '#fff', 17)
        canvas.fillText(this.data.supplierName, 22, 128, '#fff', 20,'left',true)
        // canvas.fillText('保险A方案', 22, 138, '#fff', 20,'left',true)                      
        canvas.fillText(' 微信扫描或长按识别小程序参与', 160, 360, '#fff', 13, 'center')

        canvas.drawRoundImage(100, 210, 120, this.data.qrcode, '#fff')
        ctx.draw(true)
        ctx.restore()
        // this.savecanvas()

    },
    savecanvas() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 350,
            height: 500,
            canvasId: 'myCanvas',
            success: function (res) {
                let img = res.tempFilePath
                wx.saveImageToPhotosAlbum({
                    filePath: img,
                    success() {
                        app.toast('成功保存', 'success')
                    },
                    fail() {
                        app.toast('保存失败')
                    }
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    showMorePosterStyle(){
        this.setData({
            showPosterStyle: true
        })
    }
    // onShareAppMessage(event) {
    // 	console.log('分享', event)
    // 	return {
    // 		title: user.userName + '邀请您加入「' + user.companyName + '」',
    // 		path: '/page/index/index?shareUserId=' + user.userId,
    // 	}
    // }
})